package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.core.utils.RequestUtil;
import com.csun.cmny.provider.model.domain.UacAction;
import com.csun.cmny.provider.model.domain.UacRole;
import com.csun.cmny.provider.model.dto.action.ActionMainQueryDto;
import com.csun.cmny.provider.model.dto.base.ModifyStatusDto;
import com.csun.cmny.provider.model.dto.role.ActionAssignRoleDto;
import com.csun.cmny.provider.model.dto.role.RoleQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacActionService;
import com.csun.cmny.provider.service.UacRoleService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * The class Uac action main controller.
 *
 * @author paascloud.net @gmail.com
 */
@RestController
@RequestMapping(value = "/action", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - UacActionMainController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacActionMainController extends BaseController {

	@Resource
	private UacRoleService uacRoleService;

	@Resource
	private UacActionService uacActionService;

	@Resource
	private CmnyMessageSource cmnyMessageSource;

	/**
	 * 根据功能id查询角色列表
	 *
	 * @param roleQueryDto
	 * @return
	 */
	@PostMapping(value = "/queryActionRoleList")
	public Wrapper queryActionRoleList(@RequestBody RoleQueryDto roleQueryDto) {

		roleQueryDto.setPageSize(65535);
		List<UacRole> uacRoleList = uacRoleService.queryActionRoleList(roleQueryDto);

		//return WrapMapper.ok(uacRoleList);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), uacRoleList);
	}

	/**
	 * 分配功能角色
	 *
	 * @param actionAssignRoleDto	the action assign role dto
	 *
	 * @return	the wrapper
	 */
	@PostMapping(value = "/assignRole")
	public Wrapper assignActionRole(@RequestBody ActionAssignRoleDto actionAssignRoleDto) {

		Integer result = uacRoleService.assignActionRole(actionAssignRoleDto);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 查询功能信息.
	 *
	 * @param action the action
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/queryListWithPage")
	public Wrapper queryUacActionListWithPage(@RequestBody ActionMainQueryDto action) {

		PageInfo pageInfo = uacActionService.queryActionListWithPage(action);

		//return WrapMapper.ok(pageInfo);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), pageInfo);
	}

	/**
	 * 删除角色信息.
	 *
	 * @param id the id
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/deleteById/{id}")
//	@LogAnnotation
	public Wrapper deleteUacActionById(@ApiParam(name = "id", value = "角色id")
										   @PathVariable Long id) {

		int result = uacActionService.deleteActionById(id);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 批量删除角色.
	 *
	 * @param deleteIdList the delete id list
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/batchDeleteByIdList")
	@ApiOperation(httpMethod = "POST", value = "批量删除角色")
//	@LogAnnotation
	public Wrapper batchDeleteByIdList(@ApiParam(name = "deleteIdList", value = "角色Id") @RequestBody List<Long> deleteIdList) {
		logger.info("批量删除角色 idList={}", deleteIdList);
		uacActionService.batchDeleteByIdList(deleteIdList);
		//return WrapMapper.ok();
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null));
	}

	/**
	 * 根据Id查询功能信息
	 *
	 * @param id
	 *
	 * @return
	 */
	@PostMapping(value = "/showById/{id}")
	public Wrapper showById(@PathVariable Long id) {

		UacAction uacAction = uacActionService.selectByKey(id);

		//return WrapMapper.ok(uacAction);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), uacAction);
	}


	/**
	 * 保存权限信息.
	 *
	 * @param action the action
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/save")
	@ApiOperation(httpMethod = "POST", value = "新增角色")
	@ValidateAnnotation
	//@LogAnnotation
	public Wrapper save(@ApiParam(name = "action", value = "角色信息")
							@RequestBody UacAction action, BindingResult br) {
		LoginAuthDto loginAuthDto = RequestUtil.getLoginUser();
		Integer result = uacActionService.saveAction(action, loginAuthDto);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	@PostMapping(value = "/edit")
	public Wrapper editUacAction(@RequestBody UacAction action) {

		Integer result = uacActionService.editUacAction(action, getLoginAuthDto());
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 根据权限Id修改角色状态.
	 *
	 * @param modifyStatusDto the modify status dto
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/modifyStatus")
	@ApiOperation(httpMethod = "POST", value = "根据权限Id修改角色状态")
	//@LogAnnotation
	public Wrapper modifyActionStatus(@ApiParam(name = "modifyActionStatus", value = "修改权限状态") @RequestBody ModifyStatusDto modifyStatusDto) {
		logger.info("根据角色Id修改权限状态 modifyStatusDto={}", modifyStatusDto);
		Long actionId = modifyStatusDto.getId();
		Preconditions.checkArgument(actionId != null, "权限ID不能为空");

		UacAction uacRole = new UacAction();
		uacRole.setId(actionId);
		uacRole.setStatus(modifyStatusDto.getStatus());
		uacRole.setUpdateInfo(getLoginAuthDto());

		int result = uacActionService.update(uacRole);
		return super.handleResult(result);
	}
}
